package com.xgc.book.bookserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgc.book.bookserver.bean.BookCommentBean;
import com.xgc.book.bookserver.mapper.BookCommentMapper;
import com.xgc.book.bookserver.service.BookCommentService;
import com.xgc.book.bookserver.util.ResultJson;
import com.xgc.book.bookserver.util.sensitive.SensitiveWordFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 图书评论表 服务实现类
 * </p>
 *
 * @author wrz
 * @since 2023-11-20
 */
@Service
@Transactional
public class BookCommentServiceImpl extends ServiceImpl<BookCommentMapper, BookCommentBean> implements BookCommentService {

    @Value("${sensitive.file.path}")
    private String sensitiveFilePath;

    @Override
    public ResultJson getPage(int page, int limit, BookCommentBean bookCommentBean) {
        IPage iPage = this.page(new Page<>(page, limit),
                new QueryWrapper<BookCommentBean>().eq(bookCommentBean.getBookId() != null, "book_id",
                        bookCommentBean.getBookId()).eq(bookCommentBean.getVipId() != null, "vip_id",
                        bookCommentBean.getVipId()).eq(bookCommentBean.getStatus() != null, "status",
                        bookCommentBean.getStatus()).orderByDesc("comment_time"));
        return ResultJson.ok(sensitiveFilter(iPage));
    }

    private IPage<BookCommentBean> sensitiveFilter(IPage<BookCommentBean> iPage) {
        List<BookCommentBean> list = iPage.getRecords();
        SensitiveWordFilter.loadWordFromFile(sensitiveFilePath);
        for (BookCommentBean comment : list) {
            comment.setContent(SensitiveWordFilter.Filter(comment.getContent()));
        }
        return iPage;
    }

}
